<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\wh\model;

use think\Model;
use app\admin\model\Config as ConfigModel;
use app\wh\model\Material as MaterialModel;
use think\Db;

/**
 * 预采购单模型
 */
class MaterialAinp extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'wh_material_ainp';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    // 预采购单号
    public static function getAinpSn(){

        $number = Db::name('wh_material_ainp')->order('id desc')->value('id');
        $number = intval($number)+1;

        return "XXCQY".date('Ymd').$number;
    }
    // 预采购单表单设计
    public static function getTabFrom($pid = 0,$data = [],$print = 0){

        $from = [];

        switch ($pid) {
            case 1://喷涂
            case 2://木纹
            case 3://型材
            case 5://铝板
            case 6://化工
            case 7://其他
                break;
            case 4://铝卷
                $from = self::from4($pid,$data,$print);
                break;
        }

        return $from;

    }

    //铝卷表单
    //print 0:编辑模式 1:打印模式
    public static function from4($pid = 0,$data = [],$print = 0){

        //---------------------------------------------工程表单项 start------------------------------------------
        $table_head = [["[7:1]预采购单号：{$data['ainp_sn']}<div style='text-align: right'>日期：".date('Y-m-d',$data['create_time'])."</div>"]];
        $table_data = [];

        //汇总
        $number = 0;//合计采购量
        $group = [];

        $table_data[] = [
            "<div style='text-align: center;'>名称</div>",
            "<div style='text-align: center;'>系列(类别)</div>",
            "<div style='text-align: center;'>供应商</div>",
            "<div style='text-align: center;'>厚度</div>",
            "<div style='text-align: center;'>宽度</div>",
            "<div style='text-align: center;'>材质(合金状态)</div>",
            "<div id='goods_note'><div style='text-align: center;float: left;margin: 7px auto;'>需求数量</div>
<a style='float: right;' class='btn btn-success' onclick=\"goods_add()\" ><i class='fa fa-plus-circle'></i></a>
</div>",
        ];

        foreach ($data['lists'] as $sak=>$sav){
            if(!empty($print) && 1==2){
                $table_data[] = [
                    "<div style='text-align: center;'></div>",
                ];
            }
            else{
                $table_data[] = [
                    "[0]
                    <input class='form-control goods' type='hidden' name='goods[id][]' value='{$sav['id']}'>
                    <input class='form-control goods' type='hidden' name='goods[material_id][]' value='{$sav['material_id']}'>
                    <input class='form-control goods' type='hidden' name='goods[type_pid][]' value='{$sav['type_pid']}'>
                    <input class='form-control goods' type='hidden' name='goods[row_area][]' value=''>
                    <input class='form-control goods' type='hidden' name='goods[model][]' value='{$sav['model']}'>
                    <input class='form-control goods' type='hidden' name='goods[number][]' value='{$sav['number']}'>
                    
                    ".MaterialModel::inputToText('goods[material_name][]',$sav['material_name'],$print,'','input','goods'),
                    "[0]
                    ".MaterialModel::inputToText('goods[type_id][]',$sav['type_id'],$print,'','select','goods',$data['stock_type']),
                    "[0]
                    ".MaterialModel::inputToText('goods[supplier_id][]',$sav['supplier_id'],$print,'','select','goods',$data['supplier']),
                    "[0]
                    ".MaterialModel::inputToText('goods[high][]',$sav['high'],$print,'','input','goods'),
                    "[0]
                    ".MaterialModel::inputToText('goods[long][]',$sav['long'],$print,'','input','goods'),
                    "[0]
                    ".MaterialModel::inputToText('goods[quality][]',$sav['quality'],$print,'','select','goods',$data['quality']),
                    "[0]
                    ".MaterialModel::inputToText('goods[num][]',$sav['num'],$print,'display:inline;width: 80%;','input','goods')."
                    <a class='btn btn-danger goods goods_del' onclick=\"goods_del(this)\" ><i class='fa fa-times'></i></a>
                    ",
                ];
            }

            //汇总
            if(empty($group[$sav['model']]) && !empty($sav['model']))$group[$sav['model']] = 0;
            if (isset($group[$sav['model']]))$group[$sav['model']] += $sav['num'];

        }

        //设置表单项
        $from[] = ['complexTable:12', 'order', '添加预采购单信息', $table_data, $table_head];
        //---------------------------------------------工程表单项 end--------------------------------------------


        //打印模式
        $table_data[] = ["[6:1]<div style='text-align: left;font-weight: 600;'><br>汇总要采购的信息</div>"];
        if(!empty($print))return ['head'=>$table_head,'data'=>array_merge($table_data)];
        return $from;
    }

    // 打印模板
    //print 1:打印模式 2:导出模式
    public static function toHtml($pid = 0,$data = [],$print = 1){

        $table_arr = self::getTabFrom($pid,$data,$print);

        //缩放比例
        $table_scaling = 1;

        //$html = '<html><style>th{font-size: 18px;font-weight: bold;}td{}</style><body style="margin: 4px;">';
        $html = '<html><style>th{font-size: 12px;font-weight: bold;}td{font-size: 12px;}</style><body style="margin: 4px;">';

        //头部
        /*$html .= '<table border="1" style="text-align: center;border-collapse: collapse;width: '.(21/$table_scaling).'cm;transform-origin: 0 0;"><thead>
                        <tr><th colspan="12" style="font-size: 25px;font-weight: bold;"><img style="width: 50px;margin-bottom: -20px;" class="td-none" src="'.config('template.tpl_replace_string')['__ADMIN_IMG__'].'/logo2.jpg">'.config('web_site_title').'</th></tr>
                    ';*/

        $colspan = 12;

        $html .= '<table border="1" style="text-align: center;border-collapse: collapse;transform-origin: 0 0;"><thead>
                        <tr><th colspan="'.$colspan.'" class="head" style="font-size: 25px;font-weight: bold;"><img style="width: 50px;margin-bottom: -20px;" class="td-none" src="'.config('template.tpl_replace_string')['__ADMIN_IMG__'].'/logo2.jpg">'.config('web_site_title').'</th></tr>
                    ';
        foreach ($table_arr as $tk=>$tv){
            //头
            if($tk == 'head'){
                foreach ($tv as $tvv){

                    $html .= '<tr>';
                    foreach ($tvv as $tkkk=>$tvvv){

                        $v = MaterialModel::tableParseCell($tvvv);

                        $html .= '
                            <th rowspan="'.$v['rowspan'].'" colspan="'.$v['colspan'].'">'.$v['value'].'</th>
                            ';
                        if($tkkk == 0)$colspan = $v['colspan'];
                    }
                    $html .= '</tr>';
                }
                $html .= '</thead>';
            }
            //体
            elseif($tk == 'data'){
                $html .= '<tbody>';
                foreach ($tv as $tvv){

                    $html .= '<tr>';
                    foreach ($tvv as $tvvv){

                        $v = MaterialModel::tableParseCell($tvvv);

                        //$v['value'] = str_replace("\n","<br>",$v['value']);
                        $html .= '
                            <td rowspan="'.$v['rowspan'].'" colspan="'.$v['colspan'].'">'.$v['value'].'</td>
                            ';
                    }
                    $html .= '</tr>';
                }
                $html .= '</tbody>';
            }
        }
        $html .= '</table>';

        $js = 'window.print();';
        if($print == 2){
            $js = '$("table").table2excel({
                            // 不被导出的表格行的CSS class类
                            exclude: "",
                            // 导出的Excel文档的名称
                            name: "Excel Document Name",
                            // Excel文件的名称
                            filename: "采购单",
                            //文件后缀名
                            fileext: ".xls",
                            //是否排除导出图片
                            exclude_img: false,
                            //是否排除导出超链接
                            exclude_links: true,
                            //是否排除导出输入框中的内容
                            exclude_inputs: false
                        });';
        }
        //js
        $html .= '
                    <script src="'.config('template.tpl_replace_string')['__ADMIN_JS__'].'/core/jquery.min.js?v=20180327"></script>
                    <script src="'.config('template.tpl_replace_string')['__STATIC__'].'/common/js/jquery.table2excel.js?v='.date('Ymd').'"></script>
                    <script type="text/javascript">
                        //设置表头合并
                        $(".head").attr("colspan",'.$colspan.')
                        //删除不显示的元素
                        $(".notPrint").remove()
                        //去除边框
                        $(".td-none").parent().css("border-style","hidden")
                        //设置头部边框
                        $(".td-top").parent().css("border-top","black");
                        //设置底部边框
                        $(".td-bottom").parent().css("border-bottom","black");
                        //设置高度
                        $(".td-50").parent().css("height","50px");
                        //设置向下对齐
                        $(".text-bottom").parent().css("vertical-align","bottom");
                        //设置文字上下间距
                        $(".text-height").parent().css("line-height","28px");
                        //设置文字缩放比例
                        $("th,td").css("-webkit-transform","scale('.$table_scaling.')");
                        //设置文字缩放后左对齐
                        $(".scale-left").parent().css("transform-origin","0");
                        
                        '.$js.'
                    </script>';

        return $html;
    }

}
